/**
 * Copyright (C) Skywares Information Technology, LTD. 
 * All Rights Reserved.
 *
 * MiaoshaUserService.java created on 2020年2月2日 下午12:57:19 by Lyon Lu 
 */
package com.mvc.server.miaosha.service;

import java.util.Objects;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.mvc.server.component.RedisService;
import com.mvc.server.miaosha.common.CommonContant;
import com.mvc.server.miaosha.dao.MiaoshaUserDao;
import com.mvc.server.miaosha.exception.SysException;
import com.mvc.server.miaosha.pojo.MiaoshaUser;
import com.mvc.server.miaosha.redis.MiaoshaUserKey;
import com.mvc.server.miaosha.util.MD5Util;
import com.mvc.server.miaosha.util.UUIDUtil;
import com.mvc.server.miaosha.vo.LoginVo;
import com.mvc.server.result.ResultCode;

/**
 * <pre>
 * Description:
 * 
 * @author Lyon Lu
 * @date 2020年2月2日 下午12:57:19
 *
 * </pre>
 */
@Service
public class MiaoshaUserService {
	
	@Resource
	private MiaoshaUserDao miaoshaUserDao;
	
	@Resource
	private RedisService redisService;

	/**
	 * Description:
	 * 		method description here
	 * @param response
	 * @param loginVo
	 * @return String      
	 * @throws                                 
	 */
	public String login(HttpServletResponse response, @Valid LoginVo loginVo) {
		if(Objects.isNull(loginVo)) {
			throw new SysException(ResultCode.SERVER_ERROR);
		}
		String mobile = loginVo.getMobile();
		String formPass = loginVo.getPassword();
		//判断手机号是否存在
		MiaoshaUser user = getById(Long.parseLong(mobile));
		if(Objects.isNull(user)) {
			throw new SysException(ResultCode.USER_NOT_EXIST);
		}
		//验证密码
		String dbPass = user.getPassword();
		String saltDB = user.getSalt();
		String calcPass = MD5Util.formPassToDBPass(formPass, saltDB);
		if(!calcPass.equals(dbPass)) {
			throw new SysException(ResultCode.USER_PASSWORD_ERROR);
		}
		//生成cookie
		String token = UUIDUtil.uuid();
		addCookie(response, token, user);
		return token;
	}
	
	public MiaoshaUser getById(Long id) {
		
		//取缓存
		MiaoshaUser user = redisService.get(MiaoshaUserKey.getById, String.valueOf(id), MiaoshaUser.class);
		if(Objects.nonNull(user)) {
			return user;
		}
		//取数据库
		user = miaoshaUserDao.getById(id);
		if(Objects.nonNull(user)) {
			redisService.set(MiaoshaUserKey.getById, String.valueOf(id), user);
		}
		return user;
	}
	
	public MiaoshaUser getUserByToken(HttpServletResponse response, String token) {
		
		if(StringUtils.isBlank(token)) {
			return null;
		}
		MiaoshaUser user = redisService.get(MiaoshaUserKey.token, token, MiaoshaUser.class);
		
		//延长有效期
		if(Objects.nonNull(user)) {
			addCookie(response, token, user);
		}
		return user;
	}
	
	private void addCookie(HttpServletResponse response, String token, MiaoshaUser user) {
		redisService.set(MiaoshaUserKey.token, token, user);
		Cookie cookie = new Cookie(CommonContant.COOKIE_NAME_TOKEN, token);
		cookie.setMaxAge(MiaoshaUserKey.token.expireSeconds());
		cookie.setPath("/");
		response.addCookie(cookie);
	}
}
